package android07.newtop.presenter;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;

import com.jude.beam.expansion.data.BeamDataActivityPresenter;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;

import java.util.ArrayList;
import java.util.List;

import android07.newtop.R;
import android07.newtop.activity.PictureActivity;
import android07.newtop.activity.PictureShowActivity;
import android07.newtop.adapter.PictureGalleryAdapter;
import android07.newtop.model.PictureModel;
import android07.newtop.model.bean.PictureGallery;
import android07.newtop.utils.JUtils;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static android.support.v7.widget.StaggeredGridLayoutManager.VERTICAL;


/**
 * 主页
 * Created by hy on 2016/7/7.
 */
public class PictureActivityPresenter extends BeamDataActivityPresenter<PictureActivity, PictureGallery> implements SwipeRefreshLayout.OnRefreshListener, RecyclerArrayAdapter.OnLoadMoreListener, RecyclerArrayAdapter.OnItemClickListener {

    PictureGalleryAdapter adapter;
    int page = 1;
    private List<PictureGallery.TngouBean> tngou = new ArrayList<>();
    private static final String TAG = PictureActivityPresenter.class.getSimpleName();

    /**
     * activity 刚起床， 睁开眼的那一刻 执行
     *
     * @param view
     */
    @Override
    protected void onCreateView(PictureActivity view) {
        super.onCreateView(view);
        //睁开眼，要干啥 ，要喝奶
        Log.d(TAG, "onCreateView: ");
///getDataSubscriber().onNext( 给Activity 填充数据的方法

//        tab=view.getArguments().getString("tabvalue");
        adapter = new PictureGalleryAdapter(view);
        adapter.setMore(R.layout.page_loadmore, this);
        getView().easyrecyclerview.setLayoutManager(new StaggeredGridLayoutManager(2, VERTICAL));
        getView().easyrecyclerview.setAdapterWithProgress(adapter);
        getView().easyrecyclerview.setRefreshListener(this);

        onRefresh();


    }


    @Override
    public void onRefresh() {
        new PictureModel().getPictureGallery(page, new Callback<PictureGallery>() {
            @Override
            public void onResponse(Call<PictureGallery> call, Response<PictureGallery> response) {
                adapter.clear();
                if (!tngou.containsAll(response.body().getTngou())) {
                    tngou.addAll(response.body().getTngou());
                }
                adapter.addAll(tngou);
                adapter.setOnItemClickListener(PictureActivityPresenter.this);
            }

            @Override
            public void onFailure(Call<PictureGallery> call, Throwable t) {
                JUtils.Toast("网络不给力");
                getView().easyrecyclerview.showError();
                getView().easyrecyclerview.getSwipeToRefresh().setRefreshing(false);
            }
        });


    }

    @Override
    public void onLoadMore() {
        new PictureModel().getPictureGallery(++page, new Callback<PictureGallery>() {
            @Override
            public void onResponse(Call<PictureGallery> call, Response<PictureGallery> response) {
                adapter.clear();

                if (!tngou.containsAll(response.body().getTngou())) {
                    tngou.addAll(response.body().getTngou());
                }
                adapter.addAll(tngou);

            }

            @Override
            public void onFailure(Call<PictureGallery> call, Throwable t) {
                JUtils.Toast("网络不给力");
                getView().easyrecyclerview.showError();
                getView().easyrecyclerview.getSwipeToRefresh().setRefreshing(false);
            }
        });


    }


    @Override
    public void onItemClick(int position) {
        Intent intenturl = new Intent();
        intenturl.putExtra("id", tngou.get(position).getId());
        intenturl.setClass(getView(), PictureShowActivity.class);
        getView().startActivity(intenturl);
    }
}
